package com.jrx.anytxn.transaction.mapper.ext;

import com.jrx.anytxn.transaction.entity.AmLoanAccountInfo;
import org.apache.ibatis.annotations.Param;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * selectByNextPaymentDateAndAccountStatusForBatchDistinct
 * 贷款订单账户mapper扩展
 */
public interface ExtAmLoanAccountInfoMapper {

    /**
     * 根据贷款订单ID获取贷款账户信息表
     *
     * @param loanId     贷款id
     * @param customerId 客户id
     * @param tenantId   租户id
     * @return
     */
    AmLoanAccountInfo selectByLoanId(@Param("loanId") String loanId, @Param("customerId") String customerId,
                                     @Param("tenantId") String tenantId);

    /**
     * 通过订单ID获取
     *
     * @param channel    渠道
     * @param customerId 客户ID
     * @param tenantId   渠道ID
     * @return java.math.BigDecimal
     * @author fan.yang
     * date: 2019/11/11 0011 上午 10:27
     */
    List<AmLoanAccountInfo> selectRemainPrincipalAllByLoanId(@Param("channel") String channel,
                                                             @Param("customerId") String customerId,
                                                             @Param("tenantId") String tenantId);

    /**
     * 通过订单ID获取
     *
     * @param channel    渠道
     * @param customerId 客户ID
     * @param tenantId   渠道ID
     * @param productIds   产品ID列表
     * @return java.math.BigDecimal
     * @author fan.yang
     * date: 2019/11/11 0011 上午 10:27
     */
    List<AmLoanAccountInfo> selectRemainPrincipalAllByLoanIdWithProducts(@Param("channel") String channel,
                                                             @Param("customerId") String customerId,
                                                             @Param("tenantId") String tenantId,
                                                             @Param("productIds") List<String> productIds);

    /**
     * 通过渠道状态获取订单loan_id,account_status,remain_principal
     *
     * @param channel    渠道
     * @param customerId 客户ID
     * @param tenantId   渠道ID
     * @param statusList 订单状态
     * @return java.math.BigDecimal
     * @author fan.yang
     * date: 2019/11/11 0011 上午 10:27
     */
    List<AmLoanAccountInfo> selectByStatusAndChannel(@Param("channel") String channel,
                                                     @Param("customerId") String customerId,
                                                     @Param("tenantId") String tenantId,
                                                     @Param("statusList") List<String> statusList);

    /**
     * 批量更新订单账户信息（联机可用）
     *
     * @param amLoanAccountInfoList 订单账户id
     * @return
     */
    int batchUpdateLoanAccount(List<AmLoanAccountInfo> amLoanAccountInfoList);

    /**
     * 批量更新订单账户信息（批量可用）
     *
     * @param amLoanAccountInfoList 订单账户id
     * @return
     */
    int batchUpdateLoanAccountForBatch(List<AmLoanAccountInfo> amLoanAccountInfoList);

    /**
     * 单笔更新订单账户信息
     *
     * @param loanAccountInfo
     * @return
     */
    int updateByPrimaryKey(AmLoanAccountInfo loanAccountInfo);

    /**
     * 根据下次还款日、状态查询订单账户  抛账批次使用
     *
     * @param nextPaymentDate
     * @param statusList
     * @return
     */
    List<AmLoanAccountInfo> selectByNextPaymentDateAndAccountStatusForBatch(@Param("nextPaymentDate") Date nextPaymentDate, @Param("statusList") List statusList);

    /**
     * 根据状态查询订单账户
     *
     * @param accountStatus
     * @return
     */
    List<AmLoanAccountInfo> selectByAccountStatusForBatch(@Param("accountStatus") String accountStatus);

    /**
     * 根据订单号和状态查询订单账户
     *
     * @param loanId
     * @param statusList
     * @return
     */
    AmLoanAccountInfo selectByLoanIdAndAccountStatus(@Param("loanId") String loanId,
                                                     @Param("customerId") String customerId,
                                                     @Param("tenantId") String tenantId,
                                                     @Param("statusList") List statusList);


    List<AmLoanAccountInfo> selectForUpdateUncastIntr(@Param("statusList") List<String> statusList);

    /**
     * 根据下次还款日、状态查询订单账户  批扣初始化批次使用
     *
     * @param nextPaymentDate
     * @param status
     * @return
     */
    List<AmLoanAccountInfo> selectByNextPaymentDateAndAccountStatusForBatchDistinct(@Param("nextPaymentDate") String nextPaymentDate, @Param("status") String status, @Param("productIdList") List<String> productIdList);

    /**
     * 根据客户,渠道,租户查询
     *
     * @param customerId 客户id
     * @param channel    渠道
     * @param tenantId   租户id
     * @param statusList 状态list
     * @return 贷款信息
     */
    List<AmLoanAccountInfo> selectByCustAndChannelAndStatus(@Param("customerId") String customerId,
                                                            @Param("channel") String channel,
                                                            @Param("tenantId") String tenantId,
                                                            @Param("statusList") List<String> statusList);
    /**
     * 根据客户,渠道,租户查询
     *
     * @param customerId 客户id
     * @param channel    渠道
     * @param tenantId   租户id
     * @param statusList 状态list
     * @param productIds 产品idlist
     * @return 贷款信息
     */
    List<AmLoanAccountInfo> selectByCustProductsAndChannelAndStatus(@Param("customerId") String customerId,
                                                                    @Param("channel") String channel,
                                                                    @Param("tenantId") String tenantId,
                                                                    @Param("statusList") List<String> statusList,
                                                                    @Param("productIds") List<String> productIds);

    List<AmLoanAccountInfo> selectByLoanStatusAndChannelForBatch(@Param("statusList") List<String> statusList, @Param("channel") String channel);


    /**
     * 根据产品编号，下一还款日期，贷款状态查询
     *
     * @param productId
     * @param nextPaymentDate
     * @param statusList
     * @return
     */
    List<AmLoanAccountInfo> selectByProductIdAndNextPaymentDateAndAccountStatus(@Param("productId") String productId, @Param("nextPaymentDate") Date nextPaymentDate, @Param("statusList") List<String> statusList);

    /**
     * 根据订单id集合，租户id，客户id 查询集合
     *
     * @param loanIds    订单id集合
     * @param tenantId   租户id
     * @param customerId 客户id
     * @return
     */
    List<AmLoanAccountInfo> selectByLoanIdsAndTenanIdAndCusId(@Param("loanIds") List<String> loanIds, @Param("tenantId") String tenantId, @Param("customerId") String customerId);


    List<AmLoanAccountInfo> selectByLoanIdsAndStatus(@Param("loanIds") List<String> loanIds, @Param("statusList") List<String> statusList, @Param("tenantId") String tenantId, @Param("customerId") String customerId);


    /**
     * 单笔更新订单账户信息
     *
     * @param loanAccountInfo
     * @return
     */
    int updateByLoanId(AmLoanAccountInfo loanAccountInfo);


    List<AmLoanAccountInfo> selectByAccountStatusAndProductIdForBatch(@Param("accountStatus") String accountStatus,
                                                                      @Param("productIds") List<String> productIds);


    /**
     * 根据客户，还款日，状态，产品编号查询
     *
     * @param customerId
     * @param nextPaymentDate
     * @param accountStatus
     * @return
     */
    List<AmLoanAccountInfo> selectByCustomerIdAndNextPaymentDateAndAccountStatusAndProductId(@Param(value = "tenantId") String tenantId,
                                                                                             @Param(value = "customerId") String customerId,
                                                                                             @Param(value = "nextPaymentDate") Date nextPaymentDate,
                                                                                             @Param(value = "accountStatus") String accountStatus);

    /**
     * 根据客户，还款日，状态，渠道查询
     *
     * @param customerId
     * @param nextPaymentDate
     * @param statusList
     * @param channel
     * @return
     */
    List<AmLoanAccountInfo> selectByCustomerIdAndNextPaymentDate(@Param("tenantId") String tenantId,
                                                                 @Param("customerId") String customerId,
                                                                 @Param("nextPaymentDate") Date nextPaymentDate,
                                                                 @Param("statusList") List<String> statusList,
                                                                 @Param("channel") String channel);

    /**
     * 根据客户，渠道，开始结束时间 ，排除状态 查询
     *
     * @param customerId
     * @param channel
     * @param statusList
     * @return
     */
    List<AmLoanAccountInfo> selectByCustomerIdAndLoanTypeAndNotStatus(@Param("tenantId") String tenantId,
                                                                      @Param("customerId") String customerId,
                                                                      @Param("channel") String channel,
                                                                      @Param("statusList") List<String> statusList,
                                                                      @Param("loanTypeList") List<String> loanTypeList);
    /**
     * 根据客户，渠道，开始结束时间 ，排除状态 查询
     *
     * @param customerId
     * @param channel
     * @param statusList
     * @param productIds
     * @return
     */
    List<AmLoanAccountInfo> selectByCustomerIdAndLoanTypeAndNotStatusWithProducts(@Param("tenantId") String tenantId,
                                                                      @Param("customerId") String customerId,
                                                                      @Param("channel") String channel,
                                                                      @Param("statusList") List<String> statusList,
                                                                      @Param("loanTypeList") List<String> loanTypeList,
                                                                      @Param("productIds") List<String> productIds);

    /**
     * 信用就医替换 selectByCustomerIdAndLoanTypeAndNotStatus
     * 根据客户，渠道，开始结束时间 ，排除状态 查询
     *
     * @param customerId
     * @param channel
     * @param statusList
     * @return
     */
    List<AmLoanAccountInfo> selectByCustomerIdAndLoanTypeProductIdsAndNotStatus(@Param("tenantId") String tenantId,
                                                                      @Param("customerId") String customerId,
                                                                      @Param("channel") String channel,
                                                                      @Param("statusList") List<String> statusList,
                                                                      @Param("loanTypeList") List<String> loanTypeList,
                                                                      @Param("productIds")List<String> products);


    /**
     * 根据客户,渠道,租户查询
     *
     * @param customerId 客户id
     * @param channel    渠道
     * @param tenantId   租户id
     * @param statusList 状态list
     * @param productList 产品编号
     * @return 贷款信息
     */
    List<AmLoanAccountInfo> selectByCustAndChannelAndStatusAndProductId(@Param("customerId") String customerId,
                                                            @Param("channel") String channel,
                                                            @Param("tenantId") String tenantId,
                                                            @Param("statusList") List<String> statusList,
                                                            @Param("productList") List<String> productList);

    /**
     * 根据更新时间获取贷款数据
     *
     * @param endTime
     * @param startTime
     * @return
     */
    List<AmLoanAccountInfo> selectByUpdateTimeSelective(@Param("startTime") String startTime, @Param("endTime") String endTime);

    List<AmLoanAccountInfo> selectByUpdateTime(@Param("startTime") String startTime);

    BigDecimal sumPrincipalBalance(@Param("tenantId") String tenantId,
                                   @Param("productId") String productId,
                                   @Param("statusList") List<String> statusList);


    List<AmLoanAccountInfo> selectByNextPaymentDateOrAccountStatus(@Param("productId") String productId,
                                                                   @Param("nextPaymentDate") Date nextPaymentDate,
                                                                   @Param("accountStatus") String accountStatus);

    List<AmLoanAccountInfo> selectByProIdOrAccountStatus(@Param("productId") String productId,
                                                         @Param("accountStatus") String accountStatus);

    void batchInsert(List<AmLoanAccountInfo> list);

    /**
     * 根据贷款订单ID获取贷款账户信息表
     *
     * @param loanId 贷款id
     * @return
     */
    AmLoanAccountInfo selectByLoanIdForBatch(@Param("loanId") String loanId);

    /**
     * 获取所有核销订单
     *
     * @return
     */
    List<AmLoanAccountInfo> selectAllChargeOff();

    List<AmLoanAccountInfo> selectByUpdateTimeAll();

    /**
     * 根据状态，查询订单账户
     *
     * @param accountStatus
     * @param accountStatus
     * @return
     */
    List<AmLoanAccountInfo> selectByAccountStatusAndDelqDaysForBatch(@Param("accountStatus") String accountStatus,
                                                                     @Param("delqDays") Integer delqDays);

    /**
     * 根据账户状态和结清日期查询订单id
     *
     * @param statusList 还款状态
     * @param payoffDate 结清日期
     * @param startTime  更新日期
     * @return Set<String>
     * @author liuyang
     * @date 2021-03-14
     */
    Set<String> selectLoanIdByStatus(@Param("statusList") List<String> statusList, @Param("payoffDate") Date payoffDate, @Param("startTime") Date startTime);


    /**
     * 客户订单信息统计查询(包含账单分期的)
     *
     * @param customerId
     * @param tenantId
     * @return
     */
    AmLoanAccountInfo selectMaxLoan(@Param("customerId") String customerId,
                                    @Param("tenantId") String tenantId);
    /**
     * 信用就医替换 selectMaxLoan
     * 客户订单信息统计查询(包含账单分期的)
     *
     * @param customerId
     * @param tenantId
     * @return
     */
    AmLoanAccountInfo selectMaxLoanWithProducts(@Param("customerId") String customerId,
                                    @Param("tenantId") String tenantId,
                                    @Param("productIds") List<String> productIds,@Param("businessDate") Date businessDate);

    List<AmLoanAccountInfo> selectByPostingDateAndLoanTypeForBatch(@Param("tenantId") String tenantId,
                                                                   @Param("businessDate") Date businessDate,
                                                                   @Param("loanType") String loanType,
                                                                   @Param("thirdChannel") String thirdChannel);

    int countByPayoffDateAndPayoffDate(@Param("tenantId") String tenantId,
                                       @Param("businessDate") Date businessDate,
                                       @Param("loanType") String loanType,
                                       @Param("thirdChannel") String thirdChannel,
                                       @Param("accountStatus") String accountStatus);

    BigDecimal sumByPayoffDateAndPayoffDate(@Param("tenantId") String tenantId,
                                            @Param("businessDate") Date businessDate,
                                            @Param("loanType") String loanType,
                                            @Param("thirdChannel") String thirdChannel,
                                            @Param("accountStatus") String accountStatus);

    BigDecimal sumCurrBalByCustomerId(@Param("customerId")String customerId, @Param("tenantId")String tenantId, @Param("accountStatus")List<String> accountStatus);

    /**
     * 信用就医替换 sumCurrBalByCustomerId
     * @param customerId
     * @param tenantId
     * @param accountStatus
     * @param productIds
     * @return
     */
    BigDecimal sumCurrBalByCustomerIdProducts(@Param("customerId")String customerId, @Param("tenantId")String tenantId, @Param("accountStatus")List<String> accountStatus,
                                      @Param("productIds") List<String> productIds);

    /**
     * 获取正常本金和逾期本金
     *
     * @param customerId
     * @param channel
     * @param tenantId
     * @param loanId
     * @return
     */
    List<AmLoanAccountInfo> selectLoanAcctPrinBal(@Param("customerId") String customerId, @Param("channel") String channel, @Param("tenantId") String tenantId, @Param("loanId") String loanId);

    /**
     * 获取正常本金和逾期本金
     * @param customerId
     * @param channel
     * @param tenantId
     * @param loanId
     * @param productIds
     * @param busiDate
     * @return
     */
    List<AmLoanAccountInfo> selectLoanAcctPrinBalWithProducts(@Param("customerId") String customerId, @Param("channel") String channel,
                                                              @Param("tenantId") String tenantId, @Param("loanId") String loanId,
                                                              @Param("productIds") List<String> productIds, @Param("busiDate") Date busiDate);

    /**
     * 将结清的数据余额设置为0
     * @param tenantId
     * @param loanIds
     */
    void updateLoanAcctPrinBal(@Param("tenantId") String tenantId, @Param("loanIds") List<String> loanIds);

    /**
     * 获取用户的总罚息
     * @param customerId
     * @param tenantId
     * @return
     */
    BigDecimal getUserPenaltyBalance(@Param("customerId") String customerId, @Param("tenantId") String tenantId);

    /**
     * 近三个月使用金额
     * @param tenantId
     * @param productId
     * @param customerId
     * @return
     */
    String sumLoanAmountByThreeMonth(@Param("tenantId") String tenantId, @Param("productId") String productId, @Param("customerId") String customerId);

    /**
     * 信用就医替换 selectByCustomerIdAndLoanTypeAndNotStatus
     * 根据  贷款订单ID，客户，渠道，开始结束时间 ，排除状态 查询
     *
     * @param customerId
     * @param channel
     * @param statusList
     * @return
     */
    List<AmLoanAccountInfo> selectByLoanIdAndLoanTypeProductIdsAndNotStatus(@Param("tenantId") String tenantId,
                                                                                @Param("customerId") String customerId,
                                                                                @Param("channel") String channel,
                                                                                @Param("loanId") String loanId,
                                                                                @Param("statusList") List<String> statusList,
                                                                                @Param("loanTypeList") List<String> loanTypeList,
                                                                                @Param("productIds")List<String> products);


    AmLoanAccountInfo selectMinLoanInfo(@Param("customerId") String customerId, @Param("tenantId") String tenantId, @Param("productIds") List<String> productIds,@Param("businessDate") Date businessDate);
}